package org.restoran.controller.web;

import org.restoran.model.Category;
import org.restoran.service.CategoryService;
import org.restoran.util.Utills;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
* @author Ievgen Pidvysotskyi
*/

@Controller
@RequestMapping(value = "/category", method = RequestMethod.GET, consumes = {"text/plain", "application/*"})
public class CategoryController extends AbstractController{

    private static Logger logger = LoggerFactory.getLogger(CategoryController.class);

//    todo
    private static final String ERROR_FOR_CREATION_WITH_ID
            = "\"id\":2012,  \"message\": \"Request for creating category, with settable id\"";


    private final CategoryService categoryService;

    @Autowired
    public CategoryController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }


    @RequestMapping(method = RequestMethod.GET)
    public @ResponseBody String allExistNow()
    {
        logger.debug("Request to all exist now categories");

        String s = categoryService.parseToJson(Utills.collection(categoryService.findAll()));
        return success(s);
    }

    @RequestMapping(value = "/{categoryId}", method = RequestMethod.GET)
    public @ResponseBody String findeById(
            @PathVariable("categoryId")
            Integer id)
    {
        Category category = categoryService.findOne(id);

        String json = categoryService.parseToJson(category);
        logger.debug("return json response {}", json);
        return success(json);
    }

    @RequestMapping (value = "/{id}", method = RequestMethod.PUT)
    public ResponseEntity<String> updateOne(
            @RequestBody
            String json)
    {
        Category category = categoryService.parseFromJson(json, Category.class);

        logger.debug("deserializer object {} from JSON {}, and try update", category, json);

        Category updated = categoryService.save(category);
        String respJson = categoryService.parseToJson(updated);
        return new ResponseEntity<>(success(respJson), HttpStatus.CREATED);
    }

    @RequestMapping(method = RequestMethod.POST, consumes = "application/json")
    public  ResponseEntity<String> addOne(
            @RequestBody
            String json) {
        Category parsedCategory = categoryService.parseFromJson(json, Category.class);

        logger.debug("deserializer object {} from JSON {}, and try create new", parsedCategory, json);

        if(!parsedCategory.isNew()) {
            logger.warn("Request for creation order with set id denied");
            return new ResponseEntity<>(error(ERROR_FOR_CREATION_WITH_ID), HttpStatus.UNPROCESSABLE_ENTITY);
        }

        Category created = categoryService.save(parsedCategory);
        String result = categoryService.parseToJson(created);

        logger.debug("return result for creating {}", result);

        return new ResponseEntity<>(success(result), HttpStatus.ACCEPTED);
    }



    @RequestMapping(value = "/{categoryId}", method = RequestMethod.DELETE)
    public ResponseEntity<String> delete(
            @PathVariable("categoryId")
            Integer categoryId)
    {
        categoryService.delete(categoryId);
        return new ResponseEntity<>(success(), HttpStatus.OK);
    }


}